require "common"
local STHNum = {["111"] = 1, ["222"] = 1, ["333"] = 1, ["444"] = 1,["555"] = 1, ["666"] = 1,}

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len do
                c = string.sub(num,i,i)
                if tonumber(c) > max or tonumber(c) < 1 then return nil end

        end
        return true
end


function sort_str( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, string.sub(num,i,i) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end

	return out, st

end


--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str(num)
	for i=1,len  do
		c = string.sub(str,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end

function check_k3(butType, inStr)
	local qxStr
	local qjStr
	local butNum = 0
	local butPower = 1
	local itemList = {}
	local itemLen = 0
	local nTmp = 0
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )
	if itemList[1] == '' then return 0 end

	qjStr = string.gsub( inStr, "#", "0")
	qxStr = string.gsub( qjStr, "*", "")

	if (itemLen > 2 or itemLen < 1  or  string.find(qxStr,"%D")) and butType ~= '3' and butType ~= '4' then return 0 end
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end



	--和值
	if butType == '1' then
		nTmp = tonumber(itemList[1])

		if(nTmp >=3 and nTmp <10) then
			if(string.len(itemList[1]) ~= 1) then return 0 end
		elseif(nTmp >=10 and nTmp <= 18) then
			if(string.len(itemList[1]) ~= 2) then return 0 end
		else
			return 0
		end

		butNum = 1
	--3同号单选
	elseif butType == '2' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		for i = 1,#xxItem do
			if(STHNum[xxItem[i]] ~= 1) then return 0 end
		end

		butNum = #xxItem
	--3同号通选
	elseif butType == '3' then
		if(itemList[1] ~= 'AAA') then  return 0 end
		butNum = 1
	--3连号通选
	elseif butType == '4' then
		if(itemList[1] ~= 'ABC') then  return 0 end
		butNum = 1
	--3不同号单式
	elseif butType == '5' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")
		butNum = #xxItem

		for i = 1, butNum do
			if  not judge_str(xxItem[i],6) then return 0 end
			_, nNum = get_distinct_num(xxItem[i])
			if(nNum ~= 3) then return 0 end
		end
	--3不同号复式
	elseif butType == '6' then
		if  not judge_str(itemList[1],6) then return 0 end
		_,nNum =  get_distinct_num(itemList[1])
		if (nNum ~= string.len(itemList[1])) then return 0 end
		butNum = combination(nNum, 3)
	--二同号单选
	elseif butType == '7' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")
		butNum = #xxItem

		for i = 1, butNum do
			if not judge_str(xxItem[i], 6) then return 0 end
			_,nNum =  get_distinct_num(xxItem[i])
			if nNum ~= 2 or string.len(xxItem[i]) ~= 3 then return 0 end
		end
	--二同号复选单式
	elseif butType == '8' then
		if not judge_str(itemList[1], 6) or string.len(itemList[1]) ~= 1 then return 0 end
		butNum = 1
	--二同号复选复式
	elseif butType == '9' then
		if not judge_str(itemList[1], 6) then return 0 end
		nLen = string.len(itemList[1])
		if nLen < 2 or nLen > 6 then return 0 end
		_,nNum = get_distinct_num(itemList[1])
		if nNum ~= nLen then return 0 end
		butNum = nNum
	--二不同号单式
	elseif butType == 'a' then
		if not judge_str(itemList[1], 6) then return 0 end
		_,nNum =  get_distinct_num(itemList[1])
		if nNum ~= string.len(itemList[1]) then return 0 end
		if nNum ~= 2 then return 0 end
		butNum = 1
	--二不同号复式
	elseif butType == 'b' then
		if not judge_str(itemList[1], 6) then return 0 end
		_,nNum = get_distinct_num(itemList[1])
		if nNum ~= string.len(itemList[1]) then return 0 end
		if nNum < 3 or nNum > 6 then return 0 end
		butNum = combination(nNum, 2)
	else
		return 0
	end
	return butNum, butPower, inStr, inStr

end

function query_k3(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local temp 	 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local butPower = 1
	local bingNum = 0
	local bingoD = 0
	local bingoT = 0
	local bullStr = {}

	itemList = Split( queryStr, "#" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	djStr = string.gsub(djStr, "0", "")
	for i = 1, string.len(djStr) do
		bullStr[i] = tonumber(string.sub(djStr,i,i))
	end
	--和值
	if butType == '1' then
		hezhi = bullStr[1] + bullStr[2] + bullStr[3]
		if tonumber(itemList[1]) == hezhi then
			nBingo[tonumber(itemList[1]) - 2] = 1
		end
	--3同号单选
	elseif butType == '2' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		for i = 1, #xxItem do
			if(xxItem[i] == djStr) then
				nBingo[18] = nBingo[18] + 1
			end
		end
	--3同号通选
	elseif butType == '3' then
		_,nNum = get_distinct_num(djStr)
		if nNum == 1 then
			nBingo[17] = 1
		end
	--3连号通选
	elseif butType == '4' then
		_,st = sort_str(djStr)
		if (tonumber(st[1]) + 1) == tonumber(st[2]) and (tonumber(st[2]) + 1) == tonumber(st[3]) then
			nBingo[23] = nBingo[23] + 1
		end
	--3不同号单式
	elseif butType == '5' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		djStr = sort_str(djStr)
		_,nNum = get_distinct_num(djStr)
		if nNum == 3 then
			for i = 1, #xxItem do
				xxItem[i] = sort_str(xxItem[i])
				if xxItem[i] == djStr then
					nBingo[21] = nBingo[21] + 1
				end
			end
		end
	--3不同号复式
	elseif butType == '6' then
		lotNum = {}
		djStr = sort_str(djStr)
		bingoNum = 0
		_,nNum = get_distinct_num(djStr)
		if nNum == 3 then
			for i = 1, string.len(itemList[1]) do
				lotNum[i] = tonumber(string.sub(itemList[1], i, i))
			end
			for i = 1, #lotNum do
				for j = 1, 3 do
					if(lotNum[i] == bullStr[j]) then
						bingoNum = bingoNum + 1
						break
					end
				end
			end

			if bingoNum == 3 then
				nBingo[21] = 1
			end
		end

	--二同号单选
	elseif butType == '7' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		djStr = sort_str(djStr)
		_,nNum = get_distinct_num(djStr)
		if nNum == 2 then
			for i = 1, #xxItem do
				xxItem[i] = sort_str(xxItem[i])
				if xxItem[i] == djStr then
					nBingo[20] = nBingo[20] + 1
				end
			end
		end
	--二同号复选单式
	elseif butType == '8' then
		lotNum = tonumber(itemList[1])
		bingoNum = 0
		for i = 1, 3 do
			if bullStr[i] == lotNum then
				bingoNum = bingoNum + 1
			end
		end
		if bingoNum >= 2 then
			nBingo [19] = 1
		end

	--二同号复选复式
	elseif butType == '9' then
		lotNum = {}
		bingoNum = 0
		_,nNum = get_distinct_num(djStr)
		if nNum == 2 then
			for i = 1, string.len(itemList[1]) do
				lotNum[i] = tonumber(string.sub(itemList[1], i, i))
			end
			for i = 1, #lotNum do
				bingoNum = 0
				for j = 1, 3 do
					if bullStr[j] == lotNum[i] then
						bingoNum = bingoNum + 1
					end
				end

				if bingoNum == 2 then
					nBingo[19] = nBingo[19] + 1
				end
			end
		elseif nNum == 1 then
			if string.find(itemList[1], bullStr[1]) ~= nil then
				nBingo[19] = 1
			end
		end
	--二不同号单式
	elseif butType == 'a' then
		lotNum = {}
		bullNum = {}
		bingoNum = 0
		for i = 1, string.len(itemList[1]) do
			lotNum[i] = tonumber(string.sub(itemList[1], i, i))
		end
		out,nNum = get_distinct_num(djStr)
		if(nNum >= 2) then
			for i = 1, string.len(out) do
				bullNum[i] = tonumber(string.sub(out, i, i))
			end
			for i = 1, 3 do
				for j = 1, #lotNum do
					if lotNum[j] == bullNum[i] then
						bingoNum = bingoNum + 1
					end
				end
			end
			if bingoNum >= 2 then
				nBingo[22] = 1
			end
		end
	--二不同号复式
	elseif butType == 'b' then
		lotNum = {}
		bullNum = {}
		bingoNum = 0
		for i = 1, string.len(itemList[1]) do
			lotNum[i] = tonumber(string.sub(itemList[1], i, i))
		end
		out,nNum = get_distinct_num(djStr)
		if(nNum >= 2) then
			for i = 1, string.len(out) do
				bullNum[i] = tonumber(string.sub(out, i, i))
			end
			for i = 1, 3 do
				for j = 1, #lotNum do
					if lotNum[j] == bullNum[i] then
						bingoNum = bingoNum + 1
					end
				end
			end
			if bingoNum >= 2 then
				nBingo[22] = combination(bingoNum, 2)
			end
		end
	else
		return 0
	end

	for i=1 ,23 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]..","..nBingo[9]..","..nBingo[10]..","..nBingo[11]..","..nBingo[12]..","..nBingo[13]..","..nBingo[14]..","..nBingo[15]..","..nBingo[16]..","..nBingo[17]..","..nBingo[18]..","..nBingo[19]..","..nBingo[20]..","..nBingo[21]..","..nBingo[22]..","..nBingo[23]



end


--print(check_k3('6',"1256#2"))
--print(check_k3('7',"112#2"))
--print(check_k3('9',"12#2"))
--print(check_k3('b',"123456#2"))
--print(query_k3('9',"16#4", "010105"))
